#include <stdio.h>
#include <stdlib.h>

void entab(FILE * src, FILE * dst, int width)
{

    char ch;
    int ntab = 0;
    int count = 0;
    int pos = 0;

    while ((ch=fgetc(src)) != EOF) {
        if (ch == ' ') {
            pos++;
            if (pos % width == 0) {
                ++ntab;
                count = 0;
            } else {
                ++count;
            }

        } else {
            for (;ntab>0; --ntab)
                fputc('\t', dst);

            for (;count>0; --count)
                fputc(' ', dst);

            fputc(ch, dst);

            if (ch == '\n')
                pos = 0;
            else if (ch == '\t') {
                pos += width - pos % width;

            } else {
                ++pos;
            }

        }
   }
}

int main(int argc, char **argv)
{
    FILE * fp;
    int wid = 4;

    if (argc < 2) {
        entab(stdin, stdout, wid);

    } else {
        while (--argc>0) {
            if (**(++argv) == '-')
                if (*++(*argv) == 't') {
                    wid = atoi(++*argv);
                } else {
                    fputs("参数不正确!\n", stderr);
                }

                else if ((fp=fopen(*argv, "r")) != NULL) {
                    entab(fp, stdout, wid);

                } else {
                    fprintf(stderr, "文件%s打开失败!\n", *argv);
                    return 1;
                }
        }
    }

    return 0;
}
